toNegativeIntOrNull
Returns this number as a NegativeInt, which may involve rounding or truncation, or returns null
if this number is strictly positive.
var result: NegativeInt? = (-1).toNegativeIntOrNull()
println(result) // -1
result = 0.toNegativeIntOrNull()
println(result) // 0
result = 1.toNegativeIntOrNull()
println(result) // null
Content copied to clipboard
You can use the toNegativeIntOrThrow function for throwing an IllegalArgumentException instead of returning null
when this number is strictly positive.